package net.oschina.simpleplus.web;


import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import net.oschina.simpleplus.ser.UserSer;
import net.oschina.simpleplus.util.StrUtil;

@Controller
@RequestMapping("/user")
public class UserCtrl {
	
	@Autowired
	private UserSer userSer;

	@RequestMapping(value="/login", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> login(HttpServletRequest req){
		if(StrUtil.isAnyBlank(req.getParameter("email"), req.getParameter("password"))){
			throw new RuntimeException("帐号或者密码为空！");
		}
		
		userSer.login(req.getParameter("email"), req.getParameter("password"));
		return new HashMap<>();
	}
	
	@RequestMapping(value="/reg", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> reg(HttpServletRequest req){
		if(StrUtil.isAnyBlank(req.getParameter("email"), req.getParameter("password"))){
			throw new RuntimeException("帐号或者密码为空！");
		}
		userSer.reg(req.getParameter("email"), req.getParameter("password"));
		return new HashMap<>();
	}
}
